/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector;

import com.neuronbit.businessflow.intellij.editor.swing.editor.RadComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.EventListenerList;

/**
 * The type Property editor.
 *
 * @param <V> the type parameter
 */
public abstract class PropertyEditor<V> {
  private final EventListenerList myListenerList;

    /**
     * Instantiates a new Property editor.
     */
    protected PropertyEditor(){
    myListenerList=new EventListenerList();
  }

    /**
     * Gets value.
     *
     * @return edited value. Note that {@code null} is the legal.
     * @throws Exception the exception
     */
    @Nullable
  public abstract V getValue() throws Exception;

    /**
     * Gets component.
     *
     * @param component      this component can be used to prepare editor UI component
     * @param value          value to be edited. The editor should not directly edit the passed object. Instead of this it must edit some internal data and return the edited value by {@code getValue} method.
     * @param inplaceContext this is hint for the editor. This parameter is not {@code null} in case if the editor is used for inplace editing. This hint is very useful. For example string editor doesn't have a border when it is used inside property inspector and has border when it is used for inspace editing.
     * @return the component which is used to edit the property in UI. The method must always return not {@code null} component.
     */
    public abstract JComponent getComponent(
    RadComponent component,
    V value,
    InplaceContext inplaceContext);

    /**
     * Property editor can return preferred focused component (if any) inside the component
     * which is returned by the {@link #getComponent(RadComponent, V, InplaceContext) } method.
     * This method is used as a hint to implement better focus handling.
     * {@code null} values means that editor relies on the UI editor in
     * determing preferred focused component.
     *
     * @param component cannot be null
     * @return the j component
     */
    public JComponent getPreferredFocusedComponent(@NotNull final JComponent component){
    return null;
  }

    /**
     * Editor should update UI of all its internal components to fit current
     * IDEA Look And Feel. We cannot directly update UI of the component
     * that is returned by {@link #getComponent(RadComponent, V, InplaceContext) } method
     * because hidden components that are not in the Swing tree can exist.
     */
    public abstract void updateUI();

    /**
     * Adds specified listener
     *
     * @param l the l
     */
    public final void addPropertyEditorListener(final PropertyEditorListener l){
    myListenerList.add(PropertyEditorListener.class,l);
  }

    /**
     * Removes specified listener
     *
     * @param l the l
     */
    public final void removePropertyEditorListener(final PropertyEditorListener l){
    myListenerList.remove(PropertyEditorListener.class,l);
  }

    /**
     * Fire editing cancelled.
     */
    protected final void fireEditingCancelled(){
    final PropertyEditorListener[] listeners=myListenerList.getListeners(PropertyEditorListener.class);
    for (PropertyEditorListener listener : listeners) {
      listener.editingCanceled(this);
    }
  }

    /**
     * Fire value committed.
     *
     * @param continueEditing    the continue editing
     * @param closeEditorOnError the close editor on error
     */
    protected final void fireValueCommitted(final boolean continueEditing, final boolean closeEditorOnError) {
    final PropertyEditorListener[] listeners=myListenerList.getListeners(PropertyEditorListener.class);
    for (PropertyEditorListener listener : listeners) {
      listener.valueCommitted(this, continueEditing, closeEditorOnError);
    }
  }

    /**
     * Preferred size changed.
     */
    protected final void preferredSizeChanged(){
    final PropertyEditorListener[] listeners=myListenerList.getListeners(PropertyEditorListener.class);
    for (PropertyEditorListener listener : listeners) {
      listener.preferredSizeChanged(this);
    }
  }
}
